﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Super.DomainService;

using Super.Entities;
using Super.Infrastructure;

namespace Super.DomainService
{



    #region 站点已经登录的用户 LoginUser 封装

    public class LoginUser
    {
        /// <summary> 从当前cookie中获得的UserID (不存在,则返回int.min)
        /// </summary>
        public int UserID
        {
            get { return CookieService.GetUserID(); }
        }

        /// <summary> 从当前已经登录的cookie中获得的UID
        /// </summary>
        public int UID
        {
            get { return CookieService.GetUID(); }
        }

        /// <summary> 从当前已经登录的cookie中获得UserName
        /// </summary>
        public string UserName
        {
            get { return CookieService.GetUserName(); }
        }

        public int ShopID
        {
            get { return CookieService.GetShopId(); }
        }
        /// <summary> 当前已经登录的用户实体，有数据/ 获得当前用户实体，必须从这里===切记
        /// </summary>
        public User User
        {
            get
            {
                var adp = new DbContextAdapter();
                var userService = new UserService(adp);
                var logUser = userService.GetUserById(UserID);
               
                return logUser;
            }
        }
       
    }



    #endregion


    /// <summary> 全站公共定义 service
    /// UI片段，公共定义数据，全局变量输出等，统一的，公共的共享的供应给每个web域
    /// </summary>
    public class SuperService
    {

       // public static string WebUrl = "http://www.vipjiehun.com/";
       // public static string Domain = "vipjiehun.com";

        public static string WebUrl = "";
        public static string Domain = "";

        /// <summary> 当前登录用户
        /// </summary> 
        public static LoginUser LoginUser
        {
            get
            {
                return (Common.DataCache.HttpContextCache["__LoginUser"] ?? (Common.DataCache.HttpContextCache["__LoginUser"] = new LoginUser())) as LoginUser;
            }
        }


        /// <summary> 整个错误页面的html代码
        /// 有点囧，怎么样才不囧呢
        /// </summary>
        /// <param name="errorCode"></param>
        /// <param name="errorDescription"></param>
        /// <returns></returns>
        public static string WebErrorPage(string errorCode, string errorDescription)
        {
            if (string.IsNullOrEmpty(errorCode)) errorCode = "404";
            if (string.IsNullOrEmpty(errorDescription)) errorDescription = "抱歉！出错鸟......";
            var html =
@"
<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
<style type=""text/css"">
.error { height: 250px; width: 450px; border: 1px #ccc solid; background: #f8f8f8; margin-left: auto; margin-right: auto; padding: 50px 80px; margin-top: 150px; }
.error_con { height: 220px; width: 450px; background: #fff; text-align: center; padding-top:20px;}
.error_con h3 { line-height: 80px; font-size: 25px; }
.error_con .btn { margin-left: 160px; line-height: 36px; height: 36px; width: 135px; border: none; display: block; background: #cc0000; border-radius: 3px; box-shadow: inset -1px -1px 0px #336600; color: #fff; text-align: center; cursor: pointer; font-family: ""微软雅黑"", Verdana, Geneva, sans-seri; font-weight: bold; font-size: 16px; text-decoration: none; }
</style>
<title>404</title>
</head>

<body>
<div class=""error"">
	<div class=""error_con"">
		<h3>{errorCode}{errorDescription}</h3>
	</div>
</div>
</body>
</html>
   
";
            return html.Replace("{errorCode}", errorCode).Replace("{errorDescription}", errorDescription);

        }

        /// <summary> 通过异常创建错误页面.
        /// </summary> 
        /// <param name="errorHtml"> </param>
        /// <returns></returns>
        public static string WebErrorPage(string errorHtml)
        {
            //判断是否为管理员.
            string s;
            if (true || CookieService.GetUserID() > 0 || Query.IP == "211.143.230.61" || Query.IP == "218.93.116.81" || Query.IP == "127.0.0.1" || Query.IP.StartsWith("192.168."))
            {
                s = errorHtml;
            }
            else
            {
                s = "";
            }

            return WebErrorPage("500", "糟糕，您访问的页面出现了错误！") + s;
        }

        /// <summary> 获取异常Html.
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static string GetErrorHtml(Exception ex)
        {
            var mmt = ex.StackTrace;
            var mt = ex.StackTrace.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            var s = "<div id=\"errordetail\" style=\"display:none\"><b>异常信息</b>";
            do
            {
                s += "<p>" + ex.Message + "</p>";
                ex = ex.InnerException;
            }
            while (ex != null);

            s += "<br/><br/>\r\n<table border=\"1\" rules=\"all\"><th>Method</th><th>File</th><th>Line</th>";

            for (var i = 0; i < mt.Count(); i++)
            {
                var sf = mt[i];

                var sfs = sf.Split(':');

                if (sfs.Length > 2)
                {
                    var fileName = "";
                    var methodName = "";
                    var line = "";

                    fileName = sfs[1];
                    var fn = fileName.Split('\\').ToList();
                    fn.RemoveRange(0, fn.Count - 3);

                    methodName = sfs[0].Remove(sfs[0].IndexOf(")") + 1);

                    line = sfs[2].Split(' ').Last();
                    s += "<tr><td>&nbsp&nbsp" + methodName + "&nbsp&nbsp</td><td>&nbsp&nbsp" + fn.JoinToString("/") + "&nbsp&nbsp</td><td>&nbsp&nbsp" + line + "&nbsp&nbsp</td></tr>";
                }
            }
            s += @"</table><br/><br/><div id=""errordetailex"" style=""display:none"">";

            s += mmt.Replace("\r\n", "<br/>");

            s += @"</div><button onclick=""document.getElementById('errordetailex').style.display = 'block'; "" style=""width:10px;height:10px""></button>";

            s += @"</div><button onclick=""document.getElementById('errordetail').style.display = 'block'; "" style=""width:10px;height:10px""></button>";

            return s;
        }

    }
}
